package hxy.bytecode.json;

import cn.miludeer.jsoncode.JsonCode;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

/**
 * @author eric
 * @program study
 * @description fastjson与jsoncode的区别
 * @date 2020/2/18
 */
public class JsonTest {
    private static String json = "{\"json\":{\"a\":{\"www\":\"ff\",\"rrr\":[\"v1\",\"v2\"]},\"b\":{\"www\":\"4567ttt\",\"rrr\":[\"v1\",\"v2\"]}}}";

    public static String usefastjson() {  // 使用fastjson
        JSONObject ob = JSON.parseObject(json).getJSONObject("json").getJSONObject("b");
        return ob.getString("www");
    }

    //key 里带个点不就爆炸了 "www.xxx.com": 1111
    public static String usejsoncode() {   // 使用jsoncode
        String ret = JsonCode.getValue(json, "$.json.b.www");
        return ret;
    }

    public static void main(String[] argv) {
        long time1 = System.currentTimeMillis();
        for (int i = 0; i < 10000000; i++) {
            usejsoncode();
        }
        long time2 = System.currentTimeMillis();
        for (int i = 0; i < 10000000; i++) {
            usefastjson();
        }
        long time3 = System.currentTimeMillis();


        System.out.println("fastJson:" + (time3 - time2));
        System.out.println("jsoncode:" + (time2 - time1));
    }
}
